from doub.items import DoubItem
import scrapy, pymysql


class FilmsSpider(scrapy.Spider):
    name = 'films'
    allowed_domains = ['movie.douban.com']
    start_urls = [
        'https://movie.douban.com/j/new_search_subjects?sort=U&range=0,10&tags=%E7%94%B5%E5%BD%B1&start=0&genres=%E5%89%A7%E6%83%85&countries=%E4%B8%AD%E5%9B%BD%E5%A4%A7%E9%99%86&year_range=2021,2021']


    def parse(self, response):
        item = DoubItem()
        with open("./data/data.txt", 'w+') as f:
            f.write(response.text)
        data = eval(response.text)
        for x in range(len(data['data'])):
            s = data['data'][x]
            item['directors'] = s['directors'][0]
            item['rate'] = s['rate']
            item['title'] = s['title']
            item['url'] = s['url'].replace('\\', '')
            item['casts'] = str(" ".join(s['casts']))
            item['cover'] = s['cover'].replace('\\', '')
            item['send_time'] = '2021,2021'
            # print(item)
            with open('./data/items.txt', 'a+') as f:
                f.write(str(item))
            db = pymysql.connect(
                host="localhost",
                port=3306,
                user="root",
                password="123456",
                db="test",
                charset='utf8'
            )
            sss = {'casts': '张译 于和伟 秦海璐 朱亚文 刘浩存',
                 'cover': 'https://img2.doubanio.com/view/photo/s_ratio_poster/public/p2641614433.jpg',
                 'directors': '张艺谋',
                 'rate': '7.6',
                 'send_time': '2021,2021',
                 'title': '悬崖之上',
                 'url': 'https://movie.douban.com/subject/32493124/'}
            cursor = db.cursor()
            SQL = """
            INSERT INTO `doubanfilms`(casts, cover, directors, rate, send_time, title, url) VALUES (%(casts)s,%(cover)s,%(directors)s,%(rate)s,%(send_time)s,%(title)s,%(url)s)
            """
            cursor.execute(SQL, sss)
            db.commit()
            cursor.close()
            db.close()
            # print(x,item)

            yield item
